<?php
/**
 * Created by PhpStorm.
 * User: huihui
 * Date: 2018/9/6
 * Time: 下午7:08
 */

namespace app\admin\controller;


use app\admin\model\Log;
use app\admin\model\Record;
use app\admin\model\Rewards;
use app\admin\model\Userhis;
use think\Cache;
use think\Db;
use think\Exception;

class System extends Base
{
    public function systemset()
    {
        $system = \app\admin\model\System::get(1);
        if ($updata = input("post.")){
            $system['blueoperator'] = $updata['blueoperator'];
            $system['redoperator'] = $updata['redoperator'];
            $system['company_name'] = $updata['company_name'];
            $system['domain'] = $updata['domain'];
            $system['notice'] = $updata['notice'];
            $system['fourenable'] = $updata['fourenable'];
            $system['status'] = $updata['status'];
            $r = $system->save();
            return json(['status'=>$r]);
        }
        $this->assign("system",$system);
        return view();
    }

    //打赏清零
    public function giveResult($who)
    {
        $system = \app\admin\model\System::getSystem();
        $log = new Log();
        switch ($who){
            case 0:
                $log['content'] = "清空监板分数".$system['boardScore'];
                $system['boardScore'] = 0;
                break;
            case 1:
                $log['content'] = "清空红草分数".$system['redoperator'];
                $system['redoperator'] = 0;
                break;
            case 2:
                $log['content'] = "清空红草分数".$system['blueoperator'];
                $system['blueoperator'] = 0;
                break;
        }
        $log['create_time'] = date("Y-m-d H:i:s");
        $log->save();
        $system->save();
        return json(['status'=>1]);
    }

    public function systemlog()
    {
        $datemin = input('post.datemin','');
        $datemax = input('post.datemax','');
        $this->assign("datemin",$datemin);
        $this->assign("datemax",$datemax);

        $log = new Log();
        if ($datemin && $datemax){
            $logs = $log->where('create_time','BETWEEN',[$datemin,$datemax])->order('id','desc')->select();
        }else{
            $logs = $log->order('id','desc')->select();
        }

        $this->assign("list",$logs);
        return view();
    }

    /**
     * 清理系统所有记录数据 保留用户信息
     */
    public function clearAllData()
    {
        if ($post = input('post.')){
            Db::startTrans();
            try{
                //系统日志
                $syslogs = new Log();
                $syslogs->query("TRUNCATE TABLE `log`");
//                $syslogs->where('1=1')->delete();
                //比赛信息
                $game = new \app\admin\model\Game();
                $game->query("TRUNCATE TABLE `game`");
//                $game->where('1=1')->delete();
                //用户战绩
                $record = new Record();
                $record->query("TRUNCATE TABLE `record`");
//                $record->where('1=1')->delete();
                //投注信息
                $rewards = new Rewards();
                $rewards->query("TRUNCATE TABLE `rewards`");
//                $rewards->where('1=1')->delete();
                //用户记录
                $his = new Userhis();
                $his->query("TRUNCATE TABLE `userhis`");
//                $his->where('1=1')->delete();

                $system = \app\admin\model\System::getSystem();
                $system['redoperator'] = 0;
                $system['blueoperator'] = 0;
                $system['boardScore'] = 0;
                $system->save();

                Cache::clear();

                Db::commit();
                return json(['status'=>1]);
            }catch (Exception $e){
                print_r($e);
                Db::rollback();
                return json(['status'=>0]);
            }
        }
        return view();
    }
}